<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="展示类型" prop="showType">
        <el-select v-model="queryParams.showType" placeholder="请选择展示类型" clearable>
          <el-option
            v-for="dict in file_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ty:tyBanner:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ty:tyBanner:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ty:tyBanner:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ty:tyBanner:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tyBannerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" width="200" align="center" prop="title" />
      <el-table-column label="展示类型" align="center" prop="showType">
        <template #default="scope">
          <dict-tag :options="file_type" :value="scope.row.showType"/>
        </template>
      </el-table-column>
      <el-table-column label="网页链接" width="200" align="center" prop="webUrl" />
      <el-table-column label="跳转类型" align="center" prop="webUrlType">
        <template #default="scope">
          <dict-tag :options="banner_web_url_type" :value="scope.row.webUrlType"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="跳转动作" align="center" prop="webUrlActionType">-->
<!--        <template #default="scope">-->
<!--          <dict-tag :options="banner_web_url_action_type" :value="scope.row.webUrlActionType"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="排序号" align="center" prop="sort" />
      <el-table-column label="附件" align="center" prop="image" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ty:tyBanner:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ty:tyBanner:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改首页广告对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="tyBannerRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" maxlength="20"/>
        </el-form-item>
        <el-form-item label="展示类型" prop="showType">
          <el-select v-model="form.showType" placeholder="请选择展示类型" @change="changeShowType">
            <el-option
              v-for="dict in file_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="网页链接" prop="webUrl" v-if="form.webUrlType === '3'">
          <el-input v-model="form.webUrl" placeholder="请输入网页链接" />
        </el-form-item>
        <el-form-item label="AppID" prop="webUrl" v-if="form.webUrlType === '4'">
          <el-input v-model="form.webUrl" placeholder="请输入AppID" />
        </el-form-item>
        <el-form-item label="跳转类型" prop="webUrlType">
            <el-select v-model="form.webUrlType" placeholder="请选择跳转类型" @change="changeUrlType">
              <el-option
                  v-for="dict in banner_web_url_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        <el-form-item label="类型" prop="ext1" v-if="form.webUrlType === '1'">
          <el-select v-model="form.ext1" placeholder="请选择类型" @change="changeInnerType">
            <el-option
                v-for="dict in banner_web_url_inner_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跳转至" prop="ext2"
                      v-if="form.webUrlType === '1' && (form.ext1 === '1' || form.ext1 === '2' || form.ext1 === '3' || form.ext1 === '4' || form.ext1 === '5')">
          <jump-select :key="refreshTime" v-model="form.ext2" :innerType="form.ext1"
                       :modelValue="form.ext2"></jump-select>
        </el-form-item>
        <el-form-item label="类型" prop="ext1" v-if="form.webUrlType === '2'">
          <el-select v-model="form.ext1" placeholder="请选择类型">
            <el-option
                v-for="dict in banner_web_url_tabber_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
<!--          <el-form-item label="跳转动作" prop="webUrlActionType">-->
<!--              <el-select v-model="form.webUrlActionType" placeholder="请选择跳转动作">-->
<!--                <el-option-->
<!--                    v-for="dict in banner_web_url_action_type"-->
<!--                    :key="dict.value"-->
<!--                    :label="dict.label"-->
<!--                    :value="dict.value"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
        <el-form-item label="排序号" prop="sort">
          <el-input-number v-model="form.sort" placeholder="请输入排序号" min="0" />
        </el-form-item>
        <el-form-item label="附件" prop="image">
          <image-upload v-if="form.showType === 0" :limit="1" v-model="form.image"/>
          <file-upload v-if="form.showType === 1" :limit="1" v-model="form.image"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="TyBanner">
import { listTyBanner, getTyBanner, delTyBanner, addTyBanner, updateTyBanner } from "@/api/ty/tyBanner";
import JumpSelect from "@/components/JumpSelect/index.vue";
import {ref} from "vue";

const { proxy } = getCurrentInstance();
const { file_type, banner_web_url_action_type, banner_web_url_type,banner_web_url_inner_type,banner_web_url_tabber_type } = proxy.useDict('file_type', 'banner_web_url_action_type', 'banner_web_url_type','banner_web_url_inner_type','banner_web_url_tabber_type');

const tyBannerList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const refreshTime = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    showType: null,
  },
  rules: {
    title: [
      { required: true, message: "标题不能为空", trigger: "blur" }
    ],
    showType: [
      { required: true, message: "展示类型不能为空", trigger: "change" }
    ],
    image: [
      { required: true, message: "附件不能为空", trigger: "blur" }
    ],
    // webUrlType: [
    //   { required: true, message: "跳转类型不能为空", trigger: "change" }
    // ],
    // ext1: [
    //   { required: true, message: "类型不能为空", trigger: "change" }
    // ],
    // ext2: [
    //   { required: true, message: "跳转至不能为空", trigger: "change" }
    // ],
    ext3: [
      { required: true, message: "类型不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询首页广告列表 */
function getList() {
  loading.value = true;
  listTyBanner(queryParams.value).then(response => {
    tyBannerList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    type: null,
    showType: 0,
    webUrl: null,
    webUrlType: null,
    webUrlActionType: null,
    image: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("tyBannerRef");
  refreshTime.value += 1
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加首页广告";
  refreshTime.value += 1
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getTyBanner(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改首页广告";
    refreshTime.value += 1
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["tyBannerRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateTyBanner(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTyBanner(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除该数据项？').then(function() {
    return delTyBanner(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/tyBanner/export', {
    ...queryParams.value
  }, `tyBanner_${new Date().getTime()}.xlsx`)
}

function changeShowType(){
  form.value.image = '';
}

function changeUrlType(){
  form.value.ext1 = '';
  form.value.ext2 = '';
  form.value.ext3 = '';
  form.value.webUrl = '';
}

function changeInnerType(){
  refreshTime.value += 1;
  form.value.ext2 = '';
}

getList();
</script>
